<?php
namespace App\Model;

class ArticleModel extends \App\Model\CommonModel{
    protected $tableName = 'szh_article';
    protected $primaryKey = 'itemid';

    //处理编辑时的垃圾附件
    public function treat_attachment($pk){
        $data=self::create()->where(['itemid'=>$pk])->get();
        $string=$data['thumb'].$data['content'];
        
        $model=new \App\Model\Article_attachmentModel();
        $attachments=$model->where(['articleid'=>$pk])->select();
        foreach($attachments as $k=>$v){
            if(strpos($string,$v['url'])!==false) continue;

            $file=EASYSWOOLE_ROOT.'/public'.$v['url'];
            if(!file_exists($file)) continue;
            unlink($file);
            \App\Model\Article_attachmentModel::create()->where(['itemid'=>$v['itemid']])->destroy();
        }
        return true;
    }
}